<template>
  <div id="app">
    <el-card shadow="always">
      {{studentSearch}}
      <!--条件查询-->
      <el-form :inline="true" size="medium" :model="studentSearch" class="demo-form-inline">
        <el-form-item >
          <el-input v-model="studentSearch.sname" placeholder="学生姓名"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="studentSearch.addr" placeholder="学生住址"></el-input>
        </el-form-item>
        <el-form-item >
          <el-select v-model="studentSearch.cid" placeholder="所在班级">
            <el-option label="所有班级" value="0"></el-option>
            <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="search" icon="el-icon-user">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="addStudent" icon="el-icon-plus">添加学生</el-button>
        </el-form-item>
      </el-form>
      <!--<el-divider><i class="el-icon-loading"></i></el-divider>-->
      <el-table
          :data="studentList"
          style="width: 100%">
        <el-table-column
            label="学号"
            prop="sid"
            align="center"
            width="180">
        </el-table-column>
        <el-table-column
            label="姓名"
            align="center"
            prop="sname">
        </el-table-column>
        <el-table-column
            label="性别"
            align="center"
            prop="sex">
        </el-table-column>
        <el-table-column
            label="年龄"
            align="center"
            prop="age">
        </el-table-column>
        <el-table-column
            label="住址"
            align="center"
            prop="addr">
        </el-table-column>
        <el-table-column
            label="所在班级"
            align="center"
            prop="cname">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页导航-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-card>
    <!--对话框-->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
  {{ student }}
  <el-form :model="student" :rules="formRules" ref="studentForm">
    <el-form-item label="学生姓名" :label-width="formLabelWidth" prop="sname">
      <el-input v-model="student.sname" autocomplete="off"></el-input>
    </el-form-item>
    
    <el-form-item label="学生性别" :label-width="formLabelWidth" prop="sex">
      <el-radio v-model="student.sex" label="男">男</el-radio>
      <el-radio v-model="student.sex" label="女">女</el-radio>
    </el-form-item>
    
    <el-form-item label="学生年龄" :label-width="formLabelWidth" prop="age">
      <el-input v-model="student.age" autocomplete="off" type="number"></el-input>
    </el-form-item>
    
    <el-form-item label="学生住址" :label-width="formLabelWidth" prop="addr">
      <el-input v-model="student.addr" autocomplete="off"></el-input>
    </el-form-item>
    
    <el-form-item label="所在班级" :label-width="formLabelWidth" style="width: 400px" prop="cid">
      <el-select v-model="student.cid" placeholder="请选择所在班级">
        <el-option label="所有班级" value="0"></el-option>
        <el-option :label="c.cname" :value="c.cid" v-for="c in classesList" :key="c.cid"></el-option>
      </el-select>
    </el-form-item>
  </el-form>

  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button v-if="isAdd==1" type="primary" @click="add">添加</el-button>
    <el-button v-else type="primary" @click="saveStudent">修改</el-button>
  </div>
</el-dialog>

  </div>
</template>

<script>

export default {
  data() {
    return {
      studentList: [],      // 当前页的所有学生信息集合
      page:1,               // 当前页
      pageSize:5,           // 每页大小
      total:100,            // 总记录数
      searchStudent:{},     // 要查找的学生信息对象
      studentSearch: {},    // 代表查询条件对象
      classesList: [],
      dialogFormVisible:false,  // 控制对话框的显示隐藏
      formLabelWidth:'100px',   // 对话框提示文本的宽度
      student: {},              // 学生对象（添加或修改）
      title: '添加学生',          // 对话框标题
      isAdd:1, //控制是否是添加学生还是编辑学生信息弹窗
      //校验规则
      formRules: {
      sname: [
        { required: true, message: '请输入学生姓名', trigger: 'blur' }
      ],
      sex: [
        { required: true, message: '请选择学生性别', trigger: 'change' }
      ],
      age: [
        { required: true, message: '请输入学生年龄', trigger: 'blur' },
        { min: "1", max: "100", message: '年龄必须在 1 到 100 之间', trigger: 'blur' }
      ],
      addr: [
        { required: true, message: '请输入学生住址', trigger: 'blur' }
      ],
      cid: [
        { required: true, message: '请选择所在班级', trigger: 'change' }
      ]
    }
    }
  },
  created() {
    // 1. 进行分页查询
    // this.findByPage(this.page,this.pageSize)
    // 2. 查询所有班级

    this.findClasses()
    // this.search()
    this.$post(`/student/search/${this.page}/${this.pageSize}`,{sname:this.studentSearch.sname,cid:this.studentSearch.cid,addr:this.studentSearch.addr})
      .then((res)=>{
         this.studentList = res.data.pr.rows
         this.total = res.data.pr.total
      })
  },

  methods: {
    // 1. 查询所有学生
    findStudents(){
    

    },
    // 2. 进行分页查询
         findByPage(){
    this.$get("/student/findByPage",{page:1,pageSize:5}).then((res)=>{
         this.studentList = res.rows
         this.total = res.total
    })
    },
    // 3. 查询所有班级
       findClasses(){
       this.$get("/classes/list")
       .then(res=>{
        this.classesList = res.data.list
       })
    },
    // 4. 条件查询带分页
      search(){
      console.log(this.studentSearch.value)
      this.$post(`/student/search/${this.page}/${this.pageSize}`,{sname:this.studentSearch.sname,cid:this.studentSearch.cid,addr:this.studentSearch.addr})
      .then((res)=>{
         this.studentList = res.data.pr.rows
         this.total = res.data.pr.total
      })
    },
    // 5. 添加学生
      addStudent(){
      this.dialogFormVisible = true
      this.isAdd = 1;
      console.log(this.isAdd==1)
      this.findClasses()
      this.student = {}
    },
    add(){
       console.log(this.student)
      this.$post("/student/addStudent",this.student)
      .then(res=>{
        console.log(res)
        if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
       }
      })
      .catch(err=>{
        console.log(err)
      })
    },
    // 6. 保存学生
      saveStudent(){
       
       this.$post("/student/updateStudent",this.student)
       .then(res=>{
        if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
       })
     
    },
    handleEdit(index, row) {
      this.isAdd = 0;
      console.log(index, row);
      this.dialogFormVisible = true
      this.student = row
    },
    handleDelete(index, row) {
      console.log(index);
      this.$post(`/student/deleteStudent/${index}`)
      .then(res=>{
        console.log(res)
        if (res.code === 200){
        this.$message({
          type: 'success',
          message: res.message
        })
        // 关闭对话框
        this.dialogFormVisible = false
        // 重新刷新页面
        this.search()
      }else {
        this.$message({
          type: 'error',
          message: res.message
        })
      }
      })
      .catch(err=>{
        console.log(err)
      })
    },
    // 每页大小改变时事件
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    },
    // 当前页改变时事件
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      // this.findByPage(this.page,this.pageSize)
      this.search()
    }
  }
}
</script>
<style>
  .el-card{
    width: 90%;
    margin: 0 auto;
  }
  #app{
    display: flex;
    justify-content: center;
  }
  .el-pagination{
    text-align: right;
    margin-top: 15px;
  }
</style>
